package com.btkjgc.utils;

import okhttp3.*;

import java.io.*;



/**
 * @author Post Method
 */
public class HttpPostUrlUtil {

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    static OkHttpClient client = new OkHttpClient();
    public static String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }


    /**
     * 测试主方法
     * @param args
     */
    public static void main(String[] args) {

//        Map<String, String> mapParam = new HashMap<String, String>();
//        mapParam.put("userId", "123");
//        mapParam.put("commandId", "1");
//        mapParam.put("account", "2017030316165200000111");
//        String pathUrl = "http://121.41.50.122:8080/ElectricChargeSystem/getCarInfo";
//        String result = sendPost(pathUrl, mapParam);
//        System.out.println(result);
        try {
            System.out.println(post("http://114.55.7.104/btkjsite_fuwuhao/btgateway.do", "{\n" +
                    "\"service\":\"account_change_notify_to_alipay\",\n" +
                    "\"msg\":\"HelloWorld\",\n" +
                    "\"phone\":\"15757129606\"\n" +
                    "}"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}